package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.TitledBorder;

import controller.IExchangerController;

public class PreferencesDialog extends JDialog implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private ResourceBundleWrapper resourceBundleWrapper;
	private IExchangerController control;
	private JComboBox comboBoxChooseLanguage;
	private JButton applyLanguage, cancelButton;
	private TitledBorder title;
	private JTabbedPane tabbedPane;
	
	public PreferencesDialog(IExchangerController control, ResourceBundleWrapper resourceBundleWrapper){
		super();
		this.control = control;
		this.resourceBundleWrapper = resourceBundleWrapper;
		this.setTitle(this.resourceBundleWrapper.getString("preferences"));
		this.setSize(300, 200);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.buildContentPane();
		this.setVisible(true);
	}
	
	private void buildContentPane(){
		this.tabbedPane = new JTabbedPane();
		tabbedPane.addTab(this.resourceBundleWrapper.getString("language"), buildLanguagePanel());
		tabbedPane.addTab(this.resourceBundleWrapper.getString("displaying"), buildDisplayingPanel());
		this.getContentPane().add(tabbedPane);
	}
	
	private JPanel buildLanguagePanel(){
		JPanel panel = new JPanel(new BorderLayout());
		this.title = BorderFactory.createTitledBorder(BorderFactory
				.createLineBorder(Color.black), this.resourceBundleWrapper.getString("chooseLanguage"));
		title.setTitleJustification(TitledBorder.LEFT);
		panel.setBorder(title);
		this.comboBoxChooseLanguage = new JComboBox(this.resourceBundleWrapper.getAvailableLocale());
		this.comboBoxChooseLanguage.setRenderer(new ExchangerComboBoxRenderer());
		JPanel panComboBox = new JPanel();
		panComboBox.add(this.comboBoxChooseLanguage);
		panel.add(panComboBox, BorderLayout.CENTER);
		JPanel buttonPanel = new JPanel();
		this.applyLanguage = new JButton(this.resourceBundleWrapper.getString("apply"));
		this.applyLanguage.addActionListener(this);
		this.cancelButton = new JButton(this.resourceBundleWrapper.getString("cancel"));
		this.cancelButton.addActionListener(this);
		buttonPanel.add(this.applyLanguage);
		buttonPanel.add(this.cancelButton);
		add(buttonPanel, BorderLayout.SOUTH);
		return panel;
	}
	
	private JPanel buildDisplayingPanel(){
		JPanel panel = new JPanel();
		panel.add(new JLabel("TODO"));
		return panel;
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		if(resourceBundleWrapper != null)
			updateAllTxt();
	}
	
	private void updateAllTxt(){
		this.applyLanguage.setText(this.resourceBundleWrapper.getString("apply"));
		this.title.setTitle(this.resourceBundleWrapper.getString("chooseLanguage"));
		this.tabbedPane.setTitleAt(0,this.resourceBundleWrapper.getString("language"));
		this.tabbedPane.setTitleAt(1,this.resourceBundleWrapper.getString("displaying"));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == applyLanguage){
			control.setLocale((Locale)comboBoxChooseLanguage.getSelectedItem());
		}
		else if(e.getSource() == cancelButton){
			dispose();
		}
	}
}
